<?php

namespace Common\Model;

/**
 * VipUserModel.class.php实例模型
 *
 * @author zhangzhen
 * @date 2017-2-16
 */
class VipUserModel extends BaseModel {

    protected $trueTableName = 'tb_vip_user';

    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getInfoById($id) {
        $option = [
            'id'=> $id,
        ];
        $field = 'id,hotelentity_id,vipinfo_id,name,areacode,mobile,identity';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }
     /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getLeftJoinInfoById($id) {
        $option = [
            'vip.id'=> $id,
        ];
        $field = 'vip.id,vip.hotelentity_id,vipinfo_id,vip.name as name,areacode,mobile,identity,level.name as vipname,gender,birthday,nation,address,wechat,remark';
        $data = $this->alias('vip')
                        ->field($field)
                        ->join('tb_vip_info as level ON level.id = vip.vipinfo_id','LEFT')
                        ->join('tb_vip_user_info as info ON info.vipuser_id = vip.id','LEFT')
                        ->where($option)
                        ->find();
        return empty($data) ? array() : $data;
    }
     /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getJoinVipInfoByMobile($hotelId,$mobile,$areaCode = '86') {
        $option = [
            'vip.hotelentity_id'=> $hotelId,
            'vip.mobile'=> $mobile,
            'vip.areacode'=> $areaCode,
        ];
        $field = 'vip.id,vip.hotelentity_id,vipinfo_id,vip.name as name,areacode,mobile,identity,level.name as vipname';
        $data = $this->alias('vip')
                        ->field($field)
                        ->join('tb_vip_info as level ON level.id = vip.vipinfo_id','LEFT')
                        ->where($option)
                        ->find();
        return empty($data) ? array() : $data;
    }
    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfoByMobile($hotelId,$mobile,$areacode = '86') {
        $option = [
            'hotelentity_id'=> $hotelId,
            'mobile'=> $mobile,
            'areacode'=> $areacode,
        ];
        $field = 'id,hotelentity_id,vipinfo_id,name,areacode,mobile,identity';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }
    /**
     * 返回掌柜下的会员数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfosByMobile($hotelIds,$mobile,$areaCode = '86') {
        $option = [
            'hotelentity_id'=> array('in',$hotelIds),
            'mobile'=> $mobile,
            'areacode'=> $areaCode,
        ];
        $field = 'id,hotelentity_id,vipinfo_id,name,mobile,identity';
        $data = $this->field($field)->where($option)->select();

        return empty($data) ? array() : $data;
    }
    /**
     * 手机号码模糊查询
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @param integer $areaCode
     * @return type
     */
    public function getInfoLikeMobile($hotelId,$mobile,$areaCode = '86') {
        $option = [
            'vip.hotelentity_id'=> $hotelId,
            'vip.mobile'=> array('like', $mobile.'%'),
            'vip.areacode'=> $areaCode,
        ];
        $field = 'vip.id,vip.hotelentity_id,vipinfo_id,vip.name as name,mobile,identity,level.discount as discount,level.discountrule as discountrule';
        $data = $this->field($field)
                    ->alias('vip')
                    ->join('tb_vip_info as level ON level.id = vip.vipinfo_id','LEFT')
                    ->where($option)
                    ->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 手机号码模糊查询
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfoByHotel($hotelId) {
        $option = [
            'hotelentity_id'=> $hotelId,
        ];
        $data = $this->field($field)
                    ->where($option)
                    ->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈Id获取客人信息
     * @modify zhangzhen 2017-02-14   创建
     * @param  integer   $hotelId     酒店ID
     * @param  mobile    $mobile      手机号
     * @param  integer   $offset      开始条数
     * @param  integer   $endOffset   页条数
     * @return array                信息数组
     */
    public function getVipsByHotel($hotelId, $mobile, $vipInfoId, $offset, $pageSize, $areaCode = null) {
        $option = array(
            'vip.hotelentity_id' => $hotelId,
        );
        if($mobile){
            $option['mobile'] = $mobile;
        }
        if (!is_null($areaCode)) {
            $option['areacode'] = $areaCode;
        }
        if($vipInfoId){
            $option['vipinfo_id'] = $vipInfoId;
        }
        $field = 'vip.id,vip.hotelentity_id,vipinfo_id,vip.name as name,areacode,mobile,identity,level.name as vipname';
        if(isset($offset) && $pageSize){
            $item = $this->alias('vip')
                        ->field($field)
                        ->join('tb_vip_info as level ON level.id = vip.vipinfo_id','LEFT')
                        ->where($option)
                        ->select();
            return empty($item) ? array() : $item;
        }else{
            $data = $this->alias('vip')
                        ->field($field)
                        ->join('tb_vip_info as level ON level.id = vip.vipinfo_id','LEFT')
                        ->where($option)
                        ->select();
            return empty($data) ? array() : $data;
        }
    }
    /**
     *
     * 添加用户数据
     * @modify zhangzhen 2017-02-15
     * @param integer $hotelId        客栈ID
     * @param string  $vipInfoId      等级名称Id
     * @param string  $name           会员名字
     * @param string  $mobile         电话
     * @param string  $identity       身份证
     * @param string  $areaCode       区号
     * @return type
     */
    public function addInfo($hotelId, $vipInfoId, $name, $mobile, $identity = null, $areaCode = '86') {
        $data = [
            'hotelentity_id' => $hotelId,
            'vipinfo_id' => $vipInfoId,
            'name' => $name,
            'mobile' => $mobile,
            'areacode' => $areaCode,
        ];
        if(!empty($identity)){
            $data['identity'] = $identity;
        }
        return $this->add($data);
    }

    /**
     * 更新用户数据
     * @modify zhangzhen 2017-02-16
     * @param integer $id             id
     * @param integer $vipInfoId      等级ID
     * @param string  $name           名字
     * @param string  $mobile         手机号码
     * @param string  $identity       身份证
     * @param string  $areaCode       区号
     * @return type
     */
    public function saveInfo($id, $vipInfoId, $name, $mobile, $identity, $areaCode = '') {
        $option = [];
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') !== false) {
            $option['id'] = array('in', $id);
        } else {
            $option['id'] = $id;
        }
        if(!empty($vipInfoId)){
            $data['vipinfo_id'] = $vipInfoId;
        }
        if(!empty($name)){
            $data['name'] = $name;
        }
        if(!empty($mobile)){
            $data['mobile'] = $mobile;
        }
        if(!empty($identity)){
            $data['identity'] = $identity;
        }
        if(!empty($areaCode)){
            $data['areacode'] = $areaCode;
        }

        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }

}
